package com.hkfrich.framework.config;

import com.hkfrich.framework.web.interceptor.RequestPrintInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName CorsConfig
 * @Desscription
 * @Author _HeiShuai
 * @Date 2022/8/22 17:30
 * @Version 1.0
 **/
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    /**
     * @Author _HeiShuai
     * @Description // 设置跨域后 拦截器必须注入到拦截容器中
     * @Date 11:24 2022/8/27
     * @Param [registry]
     * @return void
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestPrintInterceptor())
                .addPathPatterns("/**");
//                .excludePathPatterns("/test")
//                .order(1);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 跨域允许时间
                .maxAge(3600);
    }
}

